package com.leyantech.ecp.agenx.web.converter.output;

import com.leyantech.ai.DialogueReply;
import com.leyantech.ai.DialogueReplyContext;
import com.leyantech.ai.FAQDetail;
import com.leyantech.ai.H2HDetail;
import com.leyantech.ai.PolicyDetail;
import com.leyantech.ai.WaitDetail;
import com.leyantech.ai.WorkFlowDetail;
import com.leyantech.ecp.agenx.common.converter.output.DialogueReplyConverter;
import com.leyantech.ecp.agenx.common.utils.PbPrintHelper;
import com.leyantech.ecp.agenx.web.testwin.TestWinReply;


import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
import io.vertx.core.json.JsonObject;
import lombok.extern.slf4j.Slf4j;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-11-23.
 */
@Slf4j
public class PinDuoDuoBlockingQuestionReplyConverter implements DialogueReplyConverter {

  @Override
  public JsonObject apply(DialogueReply dialogueReply) {
    log.info("createTestWinReply with input:{}", PbPrintHelper.lazyToJson(dialogueReply));

    TestWinReply reply = new TestWinReply();

    try {
      DialogueReplyContext replyContext = dialogueReply.getContext();
      reply.setDialogueReplyContext(replyContext);
      reply.setDialogueReplyV2(dialogueReply.getDialogueReplyV2());
      Any triggerDetail = replyContext.getTriggerDetail();
      if (triggerDetail.is(PolicyDetail.class)) {
        reply.setPolicyDetail(triggerDetail.unpack(PolicyDetail.class));
      } else if (triggerDetail.is(FAQDetail.class)) {
        reply.setFaqDetail(triggerDetail.unpack(FAQDetail.class));
      } else if (triggerDetail.is(WorkFlowDetail.class)) {
        reply.setWorkFlowDetail(triggerDetail.unpack(WorkFlowDetail.class));
      } else if (triggerDetail.is(H2HDetail.class)) {
        reply.setH2HDetail(triggerDetail.unpack(H2HDetail.class));
      } else if (triggerDetail.is(WaitDetail.class)) {
        reply.setWaitDetail(triggerDetail.unpack(WaitDetail.class));
      }

    } catch (InvalidProtocolBufferException e) {
      log.error("解析测试窗 TriggerDetail错误，dialogueReply:{} ",
          PbPrintHelper.lazyToJson(dialogueReply), e);
    }

    final String replyJson = reply.toJson();
    log.info("createTestWinReply success. output:{}", replyJson);

    return new JsonObject(replyJson);
  }

}
